﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Login;

namespace UI
{
    public class ServerList : MonoBehaviour
    {
        public ServerPanel serverPanel;

        public UITriggerTab allTab;
        public UITriggerTab myTab;
        public GameObject allTip;
        public GameObject myTip;


        public GameObject allServerItemPrefab;
        public GameObject myServerItemPrefab;

        public UIGrid grid;
        public UIScrollView scrollView;
        public Transform itemCache;

        public List<UITweener> tweeners;

        Dictionary<string, GameObject> serverItems = new Dictionary<string, GameObject>();
        Dictionary<string, GameObject> myServerItems = new Dictionary<string, GameObject>();


        public ServerListType currentServerListType = ServerListType.All;

        void Start()
        {
            grid.onReposition = OnGridReposition;
        }

        void OnGridReposition()
        {
            scrollView.ResetPosition();
        }


        void Destroy()
        {
            Clear(true);
        }

        public void Hide()
        {
            gameObject.SetActive(false);
            Clear();
        }

        public void Show(ServerListType t)
        {
            gameObject.SetActive(true);
            foreach (var tweener in tweeners)
            {
                if (tweener != null)
                {
                    tweener.ResetToBeginning();
                    tweener.PlayForward();
                }
            }
            OnTabTrigger(t);
        }

        public void OnMyTabTrigger() {
            OnTabTrigger(ServerListType.My);
        }

        public void OnAllTabTrigger()
        {
            OnTabTrigger(ServerListType.All);
        }

        public void OnTabTrigger(ServerListType t)
        {
            if (t == ServerListType.All)
            {
                allTab.Trigger();
                allTip.SetActive(true);
                myTab.UnTrigger();
                myTip.SetActive(false);
                currentServerListType = t;
            }
            else if (t == ServerListType.My)
            {
                allTab.UnTrigger();
                allTip.SetActive(false);
                myTab.Trigger();
                myTip.SetActive(true);
                currentServerListType = t;
            }
            else
            {
                allTab.Trigger();
                allTip.SetActive(true);
                myTab.UnTrigger();
                myTip.SetActive(false);
                currentServerListType = ServerListType.All;
            }
            serverPanel.loginUI.manager.AskGetServerList(() => {
                InitList();
            });
        }


        void InitList()
        {
            Clear();
            if (currentServerListType == ServerListType.All)
            {
                for (int i = serverPanel.loginUI.manager.allServerInfo.Count; i > 0; i--)
                {
                    var item = serverPanel.loginUI.manager.allServerInfo[i - 1];
                    GameObject go = null;
                    if (serverItems.ContainsKey(item.globalServerKey))
                    {
                        go = serverItems[item.globalServerKey];
                    }
                    else
                    {
                        go = Instantiate(allServerItemPrefab) as GameObject;
                        serverItems[item.globalServerKey] = go;
                    }
                    go.name = "_" + item.serverId.ToString();
                    go.gameObject.SetActive(true);
                    go.GetComponent<ServerItem>().InitServerItem(item, OnServerChose);
                    go.transform.SetParent(grid.transform);
                    go.transform.localPosition = Vector3.zero;
                    go.transform.localScale = Vector3.one;
                    go.transform.localEulerAngles = Vector3.zero;
                }
                grid.repositionNow = true;
            }
            else if(currentServerListType == ServerListType.My)
            {
                for (int i = serverPanel.loginUI.manager.myServerInfo.Count; i > 0; i--)
                {
                    var item = serverPanel.loginUI.manager.myServerInfo[i - 1];
                    var baseInfo = serverPanel.loginUI.manager.GetServerInfo(item.globalServerKey);
                    if (baseInfo == null)
                    {
                        continue;
                    }
                    GameObject go = null;
                    if (myServerItems.ContainsKey(item.globalServerKey))
                    {
                        go = myServerItems[item.globalServerKey];
                    }
                    else
                    {
                        go = Instantiate(myServerItemPrefab) as GameObject;
                        myServerItems[item.globalServerKey] = go;
                    }
                    go.transform.SetParent(grid.transform);
                    go.transform.localPosition = Vector3.zero;
                    go.transform.localScale = Vector3.one;
                    go.transform.localEulerAngles = Vector3.zero;
                    go.SetActive(true);
                    go.GetComponent<MyServerItem>().InitServerItem(baseInfo, item, OnMyServerChose);
                }
                grid.repositionNow = true;
            }


        }

        public void Clear(bool isForce = false)
        {
            if (isForce)
            {
                foreach (var item in serverItems)
                {
                    Destroy(item.Value);
                }
                serverItems.Clear();
                foreach (var item in myServerItems)
                {
                    Destroy(item.Value);
                }
                myServerItems.Clear();
            }
            else
            {
                foreach (var item in serverItems)
                {
                    item.Value.transform.SetParent(itemCache);
                }
                foreach (var item in myServerItems)
                {
                    item.Value.transform.SetParent(itemCache);
                }
            }
        }

        /// <summary>
        /// 点击选择服务器,销毁服务器列表面板
        /// </summary>
        /// <param name="obj"></param>
        public void OnServerChose(GameObject obj)
        {
            var item = obj.GetComponent<ServerItem>();
            StaticData.playerData.SelectServer(item.GetZoneInfo());
            serverPanel.HideServerList();
            serverPanel.RefreshSelectServerInfo();
        }

        public void OnMyServerChose(GameObject obj)
        {
            var item = obj.GetComponent<MyServerItem>();
            StaticData.playerData.SelectServer(item.GetZoneInfo());
            serverPanel.HideServerList();
            serverPanel.RefreshSelectServerInfo();
        }

    }

}